<?php

namespace App\Http\Controllers\Api;

use App\Model\ComeBack;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class ComeBackController extends BaseController
{
    protected $comeBackModel;

    public function __construct()
    {
       $this->comeBackModel = new ComeBack();
    }

    //获取回归活动配置列表
    public function getComeBackConfigList(Request $request){
        $page = $request->get('page',1);//当前页数
        $limit = $request->get('limit',10);//每页条数
        $name = $request->get('name','');//搜索名称
        $start_time = $request->get('start_time','');//开始时间
        $end_time = $request->get('end_time','');//结束时间
        $res = $this->comeBackModel->getComeBackConfigList($page, $limit, $name, $start_time, $end_time);
        return $this->toJson(self::SUCCESS,$res);
    }

    //添加回归配置
    public function addComeBackConfig(Request $request){
        $name = $request->post('name','');//活动名称
        $icon = $request->post('icon','');//活动图标
        $start_time = $request->post('start_time','');//开始时间
        $end_time = $request->post('end_time','');//结束时间
        $type = $request->post('type',0);//上传类型
        $excelUrl = $request->post('exec_url');//文件地址
        $days = $request->post('days',0);//持续天数
        if(empty($excelUrl)){
            return $this->toJson(self::ERROR,[],'请上传文件');
        }
        $res = $this->comeBackModel->addComeBackConfig($name, $icon, $start_time, $end_time, $type, $excelUrl, $days);
        
        if($res['code'] != 0){
            return $this->toJson(self::ERROR,$res['data'],$res['msg']);
        }
        return $this->toJson(self::SUCCESS,$res['data'],$res['msg']);
    }

    //编辑活动配置
    public function editComeBackConfig(Request $request){
        $id = $request->post('id',0);//活动id
        $name = $request->post('name','');//活动名称
        $icon = $request->post('icon','');//活动图标
        $start_time = $request->post('start_time','');//开始时间
        $end_time = $request->post('end_time','');//结束时间
        $type = $request->post('type',0);//上传类型
        $excelUrl = $request->post('exec_url');//文件地址
        $days = $request->post('days',0);//持续天数
        $res = $this->comeBackModel->editComeBackConfig($id, $name, $icon, $start_time, $end_time, $type, $excelUrl, $days);
        if($res['code'] != 0){
            return $this->toJson(self::ERROR,$res['data'],$res['msg']);
        }
        return $this->toJson(self::SUCCESS,$res['data'],$res['msg']);
    }

    //删除活动配置
    public function delComeBackConfig(Request $request){
        $id = $request->post('id',0);//活动id
        $res = $this->comeBackModel->delComeBackConfig($id);
        if($res['code'] != 0){
            return $this->toJson(self::ERROR,$res['data'],$res['msg']);
        }
        return $this->toJson(self::SUCCESS,$res['data'],$res['msg']);
    }

    //查看对应活动下签到配置列表
    public function getSiginList(Request $request){
        $id = $request->post('id',0);//活动id
        $page = $request->get('page',1);//当前页数
        $limit = $request->get('limit',10);//每页条数
        if(empty($id)){
            return $this->toJson(self::ERROR,[],'缺少活动id');
        }
        $res = $this->comeBackModel->getSiginList($id, $page, $limit);
        return $this->toJson(self::SUCCESS,$res);
    }

    //添加签到配置
    public function setSiginConfig(Request $request){
        $id = $request->post('id',0);//活动id
        $day = $request->post('day',1);//天数
        $send_type = $request->post('send_type',1);//赠送类型 1=金豆 2=钻石 3=商品 4=礼物 5=许愿卡
        $goods_id = $request->post('goods_id',0);//礼物id
        $send_num = $request->post('send_num',0);//送出数量
        $img = $request->post('img','');//图片
        $award_name = $request->post('award_name','');//签到名称
        $res = $this->comeBackModel->addSiginConfig($id, $day, $send_type, $goods_id, $send_num, $img, $award_name);
        if($res['code'] != 0){
            return $this->toJson(self::ERROR,$res['data'],$res['msg']);
        }
        return $this->toJson(self::SUCCESS,$res['data'],$res['msg']);
    }

    //编辑签到配置项
    public function editSiginConfig(Request $request){
        $sigin_id = $request->post('sigin_id');//签到配置id
        $day = $request->post('day',1);//天数
        $send_type = $request->post('send_type',1);//赠送类型 1=金豆 2=钻石 3=商品 4=礼物 5=许愿卡
        $goods_id = $request->post('goods_id',0);//礼物id
        $send_num = $request->post('send_num',0);//送出数量
        $img = $request->post('img','');//图片
        $award_name = $request->post('award_name','');//签到名称
        if(empty($sigin_id)){
            return $this->toJson(self::PARAM_ERROR);
        }
        $res = $this->comeBackModel->editSiginConfig($sigin_id, $day, $send_type, $goods_id, $send_num, $img, $award_name);
        if($res['code'] != 0){
            return $this->toJson(self::ERROR,$res['data'],$res['msg']);
        }
        return $this->toJson(self::SUCCESS,$res['data'],$res['msg']);
    }

    //删除签到配置
    public function delSiginConfig(Request $request){
        $sigin_id = $request->post('sigin_id',0);//签到配置id
        if(empty($sigin_id)){
            return $this->toJson(self::PARAM_ERROR);
        }
        $res = $this->comeBackModel->delSiginConfig($sigin_id);
        if($res['code'] != 0){
            return $this->toJson(self::ERROR,$res['data'],$res['msg']);
        }
        return $this->toJson(self::SUCCESS,$res['data'],$res['msg']);
    }

    //获取每日任务列表
    public function getDayTaskList(Request $request){
        $id = $request->get('id',0);//活动id
        $page = $request->get('page',1);
        $limit = $request->get('limit',10);
        $is_day = $request->get('is_day',1);//1=日任务 0=累计任务
        if(empty($id)){
            return $this->toJson(self::ERROR,[],'缺少活动id');
        }
        $res = $this->comeBackModel->getTaskList($id, $page, $limit, $is_day);
        return $this->toJson(self::SUCCESS,$res);
    }

    //添加每日任务
    public function addDayTask(Request $request){
        $id = $request->post('id',0);//活动id
        $desc = $request->post('desc','');//任务描述
        $type = $request->post('type',1);//任务类型 1=充值2=金豆消耗3=礼物送礼消耗钻石4=房间时长
        $need_value = $request->post('need_value',0);//任务门槛
        $award_type = $request->post('award_type',0);//奖励类型1=钻石 2=金豆 3=商品 4=礼物 5=许愿卡
        $award_name = $request->post('award_name','');//奖励名称
        $award_num = $request->post('award_num',0);//奖励数量
        $award_id = $request->post('award_id',0);//奖励id(awrad_type=3,4)时候必填
        $is_day = $request->post('is_day',1);//1=每日 0=累计
        if(empty($id)){
            return $this->toJson(self::ERROR,[],'缺少活动id');
        }
        $res = $this->comeBackModel->addTask($id, $desc, $type, $need_value, $award_type, $award_name, $award_num, $award_id, $is_day);
        return $this->toJson(self::SUCCESS,$res['data'],$res['msg']);
    }

    //编辑每日任务
    public function editDayTask(Request $request){
        $task_id = $request->post('task_id',0);//任务列表id
        $desc = $request->post('desc','');//任务描述
        $type = $request->post('type',1);//任务类型 1=充值2=金豆消耗3=礼物送礼消耗钻石4=房间时长
        $need_value = $request->post('need_value',0);//任务门槛
        $award_type = $request->post('award_type',0);//奖励类型1=钻石 2=金豆 3=商品 4=礼物 5=许愿卡
        $award_name = $request->post('award_name','');//奖励名称
        $award_num = $request->post('award_num',0);//奖励数量
        $award_id = $request->post('award_id',0);//奖励id(awrad_type=3,4)时候必填
        $is_day = $request->post('is_day',1);//1=每日 0=累计
        if(empty($task_id)){
            return $this->toJson(self::ERROR,[],'缺少任务id');
        }
        $res = $this->comeBackModel->editTask($task_id, $desc, $type, $need_value, $award_type, $award_name, $award_num, $award_id, $is_day);
        return $this->toJson(self::SUCCESS,$res['data'],$res['msg']);
    }

    //删除每日任务
    public function delDayTask(Request $request){
        $task_id = $request->post('task_id',0);//任务列表id
        if(empty($task_id)){
            return $this->toJson(self::PARAM_ERROR);
        }
        $res = $this->comeBackModel->delTaskConfig($task_id);
        if($res['code'] != 0){
            return $this->toJson(self::ERROR,$res['data'],$res['msg']);
        }
        return $this->toJson(self::SUCCESS,$res['data'],$res['msg']);
    }
}